<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProductController
 *
 * @author Huynh Vinh Loc
 */
class Admin_ProductController extends Zend_Controller_Action {
    function indexAction() {        
        $product = new ProductDAO();
        $productList = $product->getProductList();
        $paginator = Zend_Paginator::factory($productList);
        $page = $this->_getParam('page',1);
        $paginator->setItemCountPerPage(4);
        $paginator->setCurrentPageNumber($page);
        $this->view->paginator = $paginator;
    }

//    function searchAction() {
//        $product = new ProductDAO();
//        $row = $product->searchProduct($this->_request->getParam('keyword'));
//        if (count($row))
//        {
//            $paginator = Zend_Paginator::factory($row);
//            $page = $this->_getParam('page',1);
//            $paginator->setItemCountPerPage(4);
//            $paginator->setCurrentPageNumber($page);
//            $this->view->paginator = $paginator;
//        }
//        else {
//            echo "No product found!";
//        }
//    }

    function addAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();
        $producer = new Producer();
        $this->view->producerList = $producer->fetchAll();
        if ($this->_request->isPost()) {
            // validating
            $array = array(
                'name' => $this->_request->getParam('name'),
                'size' => $this->_request->getParam('size'),
                'detail' => $this->_request->getParam('detail'),
                'producer_id' => $this->_request->getParam('producer_id'),
                'price' => $this->_request->getParam('price'),
                'quantity' => $this->_request->getParam('quantity'),
                'category_id' => $this->_request->getParam('category_id'),
                'warranty' => $this->_request->getParam('warranty'),
                'url' => $this->_request->getParam('url'),
                'status' => Constant::$STATUS_AVAILABLE
            );

            $validators = array(
                'name' => array('NotEmpty','messages'=>'Product name must not be empty!'),
                'url' => array('NotEmpty','messages'=>'Url must not be empty!'),
                'price' => array('Digits','messages'=>array(Zend_Validate_Digits::STRING_EMPTY=>'Price must not be empty!')),
                'quantity' => array('Digits','messages'=>array(Zend_Validate_Digits::STRING_EMPTY=>'Quantity must not be empty!')),
                'warranty' => array('Digits','messages'=>array(Zend_Validate_Digits::STRING_EMPTY=>'Warranty must not be empty!'))
            );

            $input = new Zend_Filter_Input(null, $validators, $array);

            if ($input->isValid()) {
                // adding
                $product = new ProductDAO();
                $product->addProduct($array);
                $this->_redirect('admin/product');
            }
            else {
                $allmessages = $input->getMessages();
                foreach($allmessages as $field => $messages)
                    foreach($messages as $message)            
                        echo $message."<br>";
            }
        }
    }

    function updateAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();
        $producer = new Producer();
        $this->view->producerList = $producer->fetchAll();
        $this->view->id = $this->_request->getParam('id');
        $product = new ProductDAO();
        $this->view->productList = $product->getProduct($this->_request->getParam('id'));
        if ($this->_request->isPost()) {
            // validating
            $array = array(
                'name' => $this->_request->getParam('name'),
                'size' => $this->_request->getParam('size'),
                'detail' => $this->_request->getParam('detail'),
                'producer_id' => $this->_request->getParam('producer_id'),
                'price' => $this->_request->getParam('price'),
                'quantity' => $this->_request->getParam('quantity'),
                'category_id' => $this->_request->getParam('category_id'),
                'warranty' => $this->_request->getParam('warranty'),
                'url' => $this->_request->getParam('url'),
                'status' => Constant::$STATUS_AVAILABLE
            );

            $validators = array(
                'name' => array('NotEmpty','messages'=>'Product name must not be empty!'),
                'url' => array('NotEmpty','messages'=>'Url must not be empty!'),
                'price' => array('Digits','messages'=>array(Zend_Validate_Digits::STRING_EMPTY=>'Price must not be empty!')),
                'quantity' => array('Digits','messages'=>array(Zend_Validate_Digits::STRING_EMPTY=>'Quantity must not be empty!')),
                'warranty' => array('Digits','messages'=>array(Zend_Validate_Digits::STRING_EMPTY=>'Warranty must not be empty!'))
            );

            $input = new Zend_Filter_Input(null, $validators, $array);

            if ($input->isValid()) {
                // editing
                $product = new ProductDAO();
                $product->editProduct($array);
                $this->_redirect('admin/product');
            }
            else {
                $allmessages = $input->getMessages();
                foreach($allmessages as $field => $messages)
                    foreach($messages as $message)
                        echo $message."<br>";
            }
        }
    }

    function deleteAction() {
        $product = new ProductDAO();
        $product->deleteProduct($this->_request->getParam('id'));
        $this->_redirect('admin/product');
    }
}
?>
